﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Collections.Generic;

namespace Runda.CMS.Portal.NoticeManage
{
    public partial class DutyGroupList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDutyList(AspNetPager1.CurrentPageIndex);
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindDutyList(AspNetPager1.CurrentPageIndex);
        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                SysDutyGroupEntity entity = e.Item.DataItem as SysDutyGroupEntity;
                LinkButton lnkbtnEdit = e.Item.FindControl("lnkbtnEdit") as LinkButton;
                lnkbtnEdit.Attributes.Add("onclick", "openStdWin('DutyGroupAdd.aspx?DutyGroupID="+entity.DutyGroupID+"','DutyGroupAdd');");
            }
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                SystemBll bll = new SystemBll();
                bll.Delete<SysDutyGroupEntity>(" and DutyGroupID='" + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DutyGroupID"].ToString() + "'");
            }
            BindDutyList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDutyList(AspNetPager1.CurrentPageIndex);

        }

        private void BindDutyList(int pageindex)
        {
            StringBuilder sb = new StringBuilder(); 

            if (txtDutyName.Text.Trim() != string.Empty)
            {
                sb.Append(" and DutyGroupName like '%").Append(txtDutyName.Text).Append("%'");
            }

            SystemBll bll = new SystemBll();
            int RecordCount = 0;
            List<SysDutyGroupEntity> dt = bll.GetAll<SysDutyGroupEntity>(sb.ToString(), "createdon desc", AspNetPager1.PageSize, pageindex, out RecordCount);

            AspNetPager1.RecordCount = RecordCount;
            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindDutyList(AspNetPager1.CurrentPageIndex);
        } 
    }
}
